Ext.define('Admin.view.role.manager.RoleManagerViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.roleManagerViewController',

    onAddFormSubmitButton: function(btn) {
        var form = btn.up('form').getForm();

        Ext.Ajax.request({
            url: 'role/saveOrUpdateRole',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                role: form.findField("role").getValue(),
                groups: form.findField("group").getValue()
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code === 200) {
                    btn.up('window').close();
                    Ext.getCmp('roleManagerGrid').getStore().reload();
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            },
            failure: function(response, options) {
                Ext.Msg.alert('错误', response.status + ' ' + response.statusText);
            }
        });
    },

    onEditFormSubmitButton: function(btn) {
        var form = btn.up('form').getForm(),
            record = form.getRecord();

        Ext.Ajax.request({
            url: 'role/saveOrUpdateRole',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                id: record.data.id,
                role: form.findField("role").getValue(),
                groups: form.findField("group").getValue()
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code === 200) {
                    btn.up('window').close();
                    Ext.getCmp('roleManagerGrid').getStore().reload();
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onClickRoleManagerAddUsersButton: function(btn) {
        Ext.create('widget.roleManagerFormWindow', {
            items: [{ xtype: 'roleManagerAddForm' }],
        });
    },

    onClickRoleManagerEditButton: function(view, recIndex, cellIndex, item, e, record) {
        var win = Ext.create('widget.roleManagerFormWindow', {
            items: [{ xtype: 'roleManagerEditForm' }],
        });

        var form = win.down('form').getForm();
        form.loadRecord(record);

        var groupList = [];
        Ext.each(record.data.group, function(item) {
            groupList.push(item.name);
        });

        var groups = groupList.join(',');
        form.findField('group').setRawValue(groups);
    },

    onClickRoleManagerDeleteButton: function(view, recIndex, cellIndex, item, e, record) {
        Ext.Msg.show({
            title: '确定删除?',
            message: '你确定要删除选择的角色?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(btn) {
                if (btn === 'yes') {
                    Ext.Ajax.request({
                        url: 'role/deleteRole',
                        method: 'post',
                        defaultHeaders: {
                            "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                        },
                        params: {
                            id: record.data.id
                        },
                        success: function(response, options) {
                            var json = Ext.util.JSON.decode(response.responseText);
                            if (json.code === 200) {
                                Ext.getCmp('roleManagerGrid').getStore().reload();
                            } else {
                                Ext.Msg.alert('警告', json.message);
                            }
                        },
                        failure: function(response, options) {
                            var responseText = response.statusText;
                            if (response.status == 401 || response.status == 403) {
                                responseText = '缺少权限';

                            }
                            Ext.Msg.alert('错误', response.status + ' ' + responseText);
                        }
                    });
                }
            }
        });
    },

    onClickRoleManagerDeleteUsersButton: function(btn) {
        var selected = btn.up('grid').getSelectionModel().selected.items;

        if (selected.length == 0) {
            Ext.Msg.alert('提示', '请至少选择一个角色');
            return;
        }

        Ext.Msg.show({
            title: '确定删除?',
            message: '你确定要删除选择的角色?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(btn) {
                if (btn === 'yes') {
                    var idsList = [];
                    Ext.each(selected, function(item) {
                        idsList.push(item.data.id);
                    });

                    Ext.Ajax.request({
                        url: 'role/deleteRoles',
                        method: 'post',
                        defaultHeaders: {
                            "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                        },
                        params: {
                            ids: idsList
                        },
                        success: function(response, options) {
                            var json = Ext.util.JSON.decode(response.responseText);
                            if (json.code === 200) {
                                Ext.getCmp('roleManagerGrid').getStore().reload();
                            } else {
                                Ext.Msg.alert('警告', json.message);
                            }
                        },
                        failure: function(response, options) {
                            var responseText = response.statusText;
                            if (response.status == 401 || response.status == 403) {
                                responseText = '缺少权限';

                            }
                            Ext.Msg.alert('错误', response.status + ' ' + responseText);
                        }
                    });
                }
            }
        });
    },

    onClickRoleManagerGridResetButton: function() {
        this.lookupReference('roleManagerQuery').setValue('');
        var store = Ext.getCmp('roleManagerGrid').getStore();

        Ext.apply(store.proxy.extraParams, {
            role: '',
        });

        store.load({
            params: {
                start: 0,
                limit: 20,
                page: 1
            }
        });
    },

    onClickRoleManagerGridSearchButton: function() {
        var value = this.lookupReference('roleManagerQuery').getValue();
        var store = Ext.getCmp('roleManagerGrid').getStore();

        Ext.apply(store.proxy.extraParams, {
            role: value
        });

        store.load({
            params: {
                start: 0,
                limit: 20,
                page: 1
            }
        });
    }

});